﻿using UnityEngine;

public static class TransformExtensions
{
    public static void FromMatrix(this Transform transform, Matrix4x4 matrix)
    {
        transform.localScale = matrix.ExtractScale();
        transform.localRotation = matrix.ExtractRotation();
        transform.localPosition = matrix.ExtractPosition();
    }

    public static void FromMatrix(this Transform transform, Matrix4x4 matrix, Quaternion rotation)
    {
        //        transform.localScale = matrix.ExtractScale();
        transform.localRotation = rotation;
        Matrix4x4 tempMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Inverse(rotation), Vector3.one) * matrix;
        transform.localScale = new Vector3(tempMatrix.m00, tempMatrix.m11, tempMatrix.m22);

        transform.localPosition = matrix.ExtractPosition();
    }

    public static void CopyLocal(this Transform transform, Transform from)
    {
        transform.localScale = from.localScale;
        transform.localRotation = from.localRotation;
        transform.localPosition = from.localPosition;
    }

    public static void Copy(this Transform transform, Transform from)
    {
        transform.localScale = from.localScale;
        transform.rotation = from.rotation;
        transform.position = from.position;
    }

    public static void ResetLocalPosition(this Transform transform)
    {
        transform.localPosition = Vector3.zero;
    }

    public static void ResetLocalRotation(this Transform transform)
    {
        transform.localRotation = Quaternion.identity;
    }

    public static void ResetLocalScale(this Transform transform)
    {
        transform.localScale = Vector3.one;
    }

    public static void ResetPosition(this Transform transform)
    {
        transform.position = Vector3.zero;
    }

    public static void ResetRotation(this Transform transform)
    {
        transform.rotation = Quaternion.identity;
    }

    public static void ResetLocalTRS(this Transform transform)
    {
        transform.ResetLocalPosition();
        transform.ResetLocalRotation();
        transform.ResetLocalScale();
    }

    public static void SetPosition(this Transform transform, float x, float y, float z)
    {
        transform.position = new Vector3(x, y, z);
    }

    public static float GetPosition(this Transform transform, out float y, out float z)
    {
        Vector3 p = transform.position;
        y = p.y;
        z = p.z;
        return p.x;
    }

    public static void SetLocalPosition(this Transform transform, float x, float y, float z)
    {
        transform.localPosition = new Vector3(x, y, z);
    }

    public static float GetLocalPosition(this Transform transform, out float y, out float z)
    {
        Vector3 p = transform.localPosition;
        y = p.y;
        z = p.z;
        return p.x;
    }



    public static void SetRotation(this Transform transform, float x, float y, float z, float w)
    {
        transform.rotation = new Quaternion(x, y, z, w);
    }

    public static float GetRotation(this Transform transform, out float y, out float z, out float w)
    {
        Quaternion p = transform.rotation;
        y = p.y;
        z = p.z;
        w = p.w;
        return p.x;
    }

    public static void SetLocalRotation(this Transform transform, float x, float y, float z, float w)
    {
        transform.localRotation = new Quaternion(x, y, z, w);
    }

    public static float GetLocalRotation(this Transform transform, out float y, out float z, out float w)
    {
        Quaternion p = transform.localRotation;
        y = p.y;
        z = p.z;
        w = p.w;
        return p.x;
    }

    public static void SetLocalScale(this Transform transform, float x, float y, float z)
    {
        transform.localScale = new Vector3(x, y, z);
    }

    public static float GetLocalScale(this Transform transform, out float y, out float z)
    {
        Vector3 p = transform.localScale;
        y = p.y;
        z = p.z;
        return p.x;
    }

    public static void SetRotationFaceMainCamera(this Transform transform)
    {
        transform.rotation = UnityEngine.Camera.main.transform.rotation;
    }

    public static float InverseTransformPointEx(this Transform transform, float x, float y, float z, out float oy, out float oz)
    {
        Vector3 ret = transform.InverseTransformPoint(new Vector3(x, y, z));
        oy = ret.y;
        oz = ret.z;
        return ret.x;
    }

    public static float RotationEulerAnglesY(this Transform trans)
    {
        return trans.rotation.eulerAngles.y;
    }
}
